// pages/order/list.js
const api = require("../../request/api")
const request = require("../../request/request")
const util = require("../../utils/util")
const App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title_list: [{ name: "全部" }, { name: "待付款" }, { name: "已支付" }, { name: "待收货", }, { name: "已完成" }],
    chosen_index: 0,
    all_list: [],
    waiting_for_paying_list: [],
    loading_text: "暂无数据",
    uid: 9,
    order_status: ['待付款', '待付款', '已支付', '待收货', '', '已完成'],
    winHeight: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      uid: util.getAppValue(App.globalData.uid)
    })

    util.showLoading('正在加载')

    let chosen_index = options.index

    this.setData({
      chosen_index
    })

    this.get_order_list()
  },

  changing_tab(e) {
    this.setData({
      chosen_index: e.currentTarget.dataset.index
    })
    this.get_order_list()
  },

  get_order_list() {
    util.showLoading('正在加载')
    let params = {
      uid: this.data.uid,
      type: "costpoint",
    }
    // 调整参数，根据chosen_index来决定status的值
    params = this.set_status(params)
    request.post(api.orderlist, params).then(res => {
      this.setData({
        all_list: res,
      })
      util.hideLoading()
    })
  },

  set_status(params) {
    let chosen_index = this.data.chosen_index
    if (chosen_index == 1) {
      params.status = 1
    }
    if (chosen_index == 2) {
      params.status = 2
    }
    if (chosen_index == 3) {
      params.status = 3
    }
    if (chosen_index == 4) {
      params.status = 4
    }

    return params
  },
  go_order_detail(e) {
    let id = e.currentTarget.dataset.id

    let url = '/pages/order/order_detail?id=' + id
    wx.navigateTo({
      url,
    })
  },
  changing_swiper_item(e) {
    this.setData({
      chosen_index: e.detail.current
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})